<?php
require_once("include/bittorrent.php");
loggedinorreturn();
parked();
if (@ini_get('output_handler') == 'ob_gzhandler' AND @ob_get_length() !== false) {
	@ob_end_clean();
	header('Content-Encoding:');
}

if (!preg_match('%^/(\d{1,10})/(.+)\.torrent$%si', $_SERVER["PATH_INFO"], $matches)) httperr();

$id = intval($_GET["id"]);
if (!is_numeric($id)) stderr($tracker_lang['error'],$tracker_lang['invalid_id']);

$res = sql_query("SELECT name FROM torrents WHERE id = ".sqlesc($id)) or sqlerr(__FILE__, __LINE__);
$row = mysql_fetch_assoc($res);
if (!$row) stderr($tracker_lang['error'], $tracker_lang['invalid_id']);

if ( antiLeech($id) ) stderr($tracker_lang['Your rating is too small for downloads']);

$fn = "$options[torrent_dir]/$id.torrent";

if (!$row || !is_file($fn) || !is_readable($fn))
	stderr($tracker_lang['error'], $tracker_lang['unable_to_read_torrent']);

sql_query('UPDATE torrents SET hits = hits + 1 WHERE id = '.sqlesc($id));

require_once "include/benc.php";

if (strlen($CURUSER['passkey']) != 32) {
	$CURUSER['passkey'] = md5($CURUSER['username'].get_date_time().$CURUSER['passhash']);
	sql_query("UPDATE users SET passkey=".sqlesc($CURUSER[passkey])." WHERE id=".sqlesc($CURUSER[id]));
}

$dict = bdec_file($fn, (1024*1024));

$dict['value']['announce']['value'] = $announce_urls[0]."?passkey=$CURUSER[passkey]";
$dict['value']['announce']['string'] = strlen($dict['value']['announce']['value']).":".$dict['value']['announce']['value'];
$dict['value']['announce']['strlen'] = strlen($dict['value']['announce']['string']);

header ("Expires: Tue, 1 Jan 1980 00:00:00 GMT");
header ("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache");
//header ("X-Powered-by: TBDev Yuna Scatari Edition - http://bit-torrent.kiev.ua");
header ("Accept-Ranges: bytes");
header ("Connection: close");
header ("Content-Transfer-Encoding: binary");
header ("Content-Disposition: attachment; filename=\"".$row['name']."\"");
header ("Content-Type: application/x-bittorrent");
ob_implicit_flush(true);

print(benc($dict));
?>
